<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class InstallController extends Controller
{
	
	private function _checkInitDB(){
				if(Yii::app()->params['app']['initialized']){
			if(User::model()->count()==0){
				$this->redirect(Yii::app()->createUrl("install/setup3"));
				return false;
			}else{
				$this->redirect(Yii::app()->createUrl("site/index"));
			}
			return false;
		}

		return true;
	}
	
	private function _checkInitUser(){
		if(Yii::app()->params['app']['initialized']==false){
						$this->redirect(Yii::app()->createUrl("install/index"));
			return false;
		}
		if(User::model()->count()>1){
			$this->redirect(Yii::app()->createUrl("install/index"));
			return false;
		}
		return true;

	}

	public function actionIndex()
	{
		if($this->_checkInitDB()){
			$this->layout='//layouts/install';
			$this->render('index');
		}
	}
	
	public function actionSetup2()
	{
		if($this->_checkInitDB()){
			$this->layout='//layouts/install';
			$model = new Setup2Form();
			if(Yii::app()->request->isPostRequest){
				$model->attributes=$_POST['Setup2Form'];
				if($model->save()){
					$this->redirect(Yii::app()->createUrl("install/setup3"));
				}
			}
			$this->render('setup2',array(
                'model'=>$model, 
			));
		}
	}
	
	public function actionSetup3()
	{
		if($this->_checkInitUser()){
			$this->layout='//layouts/install';
			$model = new Setup3Form();
			if(Yii::app()->request->isPostRequest){
				$model->attributes=$_POST['Setup3Form'];
				if($model->save()){
					if (SYSTEM_STATUS == "init" && $model->password != "admin") {
						SystemManager::addUserSuccess();
					}
					$this->redirect(Yii::app()->createUrl("install/setup4"));
				}
			}
			$this->render('setup3',array(
                'model'=>$model, 
			));
		}
	}
	
	public function actionSetup4()
	{
			
		$last = new SetupLast();
		$last->deviceRecord();

		$this->layout='//layouts/install';
		$this->render('setup4',array(
		));
	}
}